package shuishijie.suishijie.model;


import android.os.Message;
import android.text.TextUtils;




import java.io.File;
import java.net.URLEncoder;
import java.util.List;


import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import shuishijie.suishijie.model.web.BaseResponse;
import shuishijie.suishijie.model.web.GvgListResponse;
import shuishijie.suishijie.model.web.GvgService;
import shuishijie.suishijie.model.web.ListResponse;
import shuishijie.suishijie.model.web.RetrofitManager;
import shuishijie.suishijie.model.web.WebManager;
import shuishijie.suishijie.util.AppUtil;

/**
 * Created by mindx on 2017/4/20.
 */

public class GvgModel extends CommonData {
    GvgService retrofit;
    static GvgModel instance;

    public static GvgModel getInstance() {
        if (instance == null) {
            instance = new GvgModel();
        }
        return instance;
    }


    protected GvgModel() {
        retrofit = RetrofitManager.getInstance().build().create(GvgService.class);
    }

    public Observable<BaseResponse> goldCoinList(int coinStatus) {
        return retrofit.goldCoinList(mobile, token, user_id, coinStatus)
                .compose(WebManager.allTransformer());
// V2
//           .compose(WebManager.sTransformer())
//                .compose(WebManager.switchSchedulers());
//
// V1
//                .flatMap(WebManager::flatResult)
//                .onErrorResumeNext(new WebManager.HttpResponseFunc<>());
    }

//    /**
//     * 首页头部
//     **/
//    public Observable<BaseResponse<List<Navigation>>> navigationList() {
//        return retrofit.navigationList(null)
//                .compose(WebManager.allTransformer());
//    }


//    /**
//     * 首页广告轮播
//     **/
//    public Observable<BaseResponse<List<ImgData>>> advImages() {
//        return retrofit.advImages()
//                .compose(WebManager.allTransformer());
//    }

//    /**
//     * 首页-推荐活动展示
//     **/
//    public Observable<BaseResponse<List<ActivityDetail>>> showActivity() {
//        return retrofit.showActivity()
//                .compose(WebManager.allTransformer());
//    }
//
//
//    /**
//     * 首页-限时抢购展示
//     **/
//    public Observable<BaseResponse<HotTime>> showHotTime() {
//        return retrofit.showHotTime()
//                .compose(WebManager.allTransformer());
//    }
//
//    /**
//     * 首页-推荐品牌和推荐单品显示
//     **/
//    public Observable<BaseResponse<HomeRecommand>> recommendGoodsAndBrand() {
//        return retrofit.recommendGoodsAndBrand(mobile, token, shop_id)
//                .compose(WebManager.allTransformer());
//    }
//
//    /**
//     * 商品详情
//     **/
//    public Observable<BaseResponse<GoodsDetail>> goodsDetails(String goods_id) {
//        return retrofit.goodsDetails(mobile, token, goods_id, shop_id)
//                .compose(WebManager.allTransformer());
//    }
//
//    /**
//     * 商品分类展示
//     **/
//    public Observable<BaseResponse<List<GoodsCategory>>> goodsclassify() {
//        return retrofit.goodsclassify()
//                .compose(WebManager.allTransformer());
//    }
//
//    /**
//     * 加入购物车
//     **/
//    public Observable<BaseResponse<ShoppingCar>> addToShoppingCar(String goods_id, String spec_id, String status, String account) {
//        return retrofit.addToShoppingCar(mobile, token, user_id, goods_id, shop_id, spec_id, status, account)
//                .compose(WebManager.allTransformer());
//    }
//
//    /**
//     * 商品列表展示
//     **/
//    public Observable<BaseResponse<GoodsList>> goodsList(String searchInfo, String brand_id, String classify_id, String sort, String currentPage) {
//        return retrofit.goodsList(mobile, token, searchInfo, brand_id, classify_id, PAGE_COUNT, currentPage, sort, shop_id)
//                .compose(WebManager.allTransformer());
//    }
//
//    public Observable<BaseResponse<GoodsList>> goodsList(String classify_id, String currentPage) {
//        return retrofit.goodsList(mobile, token, null, null, classify_id, PAGE_COUNT, currentPage, null, shop_id)
//                .compose(WebManager.allTransformer());
//    }
//
//    /**
//     * 品牌列表展示
//     **/
//    public Observable<BaseResponse<BrandList>> brandsList(String currentPage) {
//        return retrofit.brandsList(mobile, token, currentPage, shop_id, PAGE_COUNT)
//                .compose(WebManager.allTransformer());
//    }
//
//    /**
//     * showActivityGoods
//     **/
//    public Observable<BaseResponse<GoodsList>> showActivityGoods(String activity_id) {
//        return retrofit.showActivityGoods(mobile, token, activity_id, shop_id)
//                .map(data -> {
//                    BaseResponse<GoodsList> res = new BaseResponse<GoodsList>(data);
//                    res.data = new GoodsList(data.data);
//                    return res;
//                })
//                .compose(WebManager.allTransformer());
//    }
//
//    /**
//     * 添加店主精选商品或品牌
//     **/
//    public Observable<BaseResponse<ShopInfo>> saveBrandOrGoods(String goods_id, String brand_id) {
//        return retrofit.saveBrandOrGoods(mobile, token, shop_id, goods_id, brand_id)
//                .compose(WebManager.allTransformer());
//    }
//
//    /**
//     * removeBrandOrGoods
//     **/
//    public Observable<BaseResponse<Object>> removeBrandOrGoods(String id) {
//        return retrofit.removeBrandOrGoods(mobile, token, user_id, id)
//                .compose(WebManager.allTransformer());
//    }
//
//
//    /**
//     * 购物车列表展示
//     **/
//    public Observable<BaseResponse<List<ShoppingCarInfo>>> shoppingCarList() {
//        return retrofit.shoppingCarList(mobile, token, user_id, shop_id)
//                .compose(WebManager.allTransformer());
//    }

    /**
     * 修改购物车数量
     **/
    public Observable<BaseResponse<Object>> updateShoppingCarAccount(String shoppingCar_id, String spec_id, String account) {
        return retrofit.updateShoppingCarAccount(mobile, token, user_id, shoppingCar_id, spec_id, account)
                .compose(WebManager.allTransformer());
    }

    /**
     * 删除购物车
     *
     * @param cartIds
     */
    public Observable<BaseResponse<Object>> delShoppingCar(List<String> cartIds) {
        return retrofit.delShoppingCar(mobile, token, user_id, TextUtils.join(",", cartIds))
                .compose(WebManager.allTransformer());
    }

    /**
     * 确认订单页初始化
     **/
    public Observable<BaseResponse<OrderInfo>> queryOrderConfirm(List<String> carIds) {
        return retrofit.queryOrderConfirm(mobile, token, user_id, shop_id, TextUtils.join(",", carIds))
                .compose(WebManager.allTransformer());
    }

//    /**
//     * 地址列表展示
//     **/
//    public Observable<BaseResponse<List<AddressInfo>>> showAddressList() {
//        return retrofit.showAddressList(mobile, token, user_id)
//                .compose(WebManager.allTransformer());
//    }

    /**
     * 生成订单
     **/
    public Observable<BaseResponse<OrderInfo>> saveGoodsOrder(String coin_id, String coupon_id, String addr_id, String transport_id, List<String> carIds) {
        return retrofit.saveGoodsOrder(mobile, token, user_id, shop_id, coin_id, coupon_id, addr_id, transport_id, TextUtils.join(",", carIds), "app")
                .compose(WebManager.allTransformer());
    }

    /**
     * 删除地址
     **/
    public Observable<BaseResponse<Object>> removeAddress(String address_id) {
        return retrofit.removeAddress(mobile, token, user_id, address_id)
                .compose(WebManager.allTransformer());
    }

    /**
     * 设置默认地址
     **/
    public Observable<BaseResponse<Object>> defaultAddress(String address_id) {
        return retrofit.defaultAddress(mobile, token, user_id, address_id)
                .compose(WebManager.allTransformer());
    }

    /**
     * 添加和修改地址
     **/
    public Observable<BaseResponse<Object>> saveAndUpdateAddress(String address_id, String name, String phone, String identification, String province_id, String city_id, String district_id, String addressInfo) {
        return retrofit.saveAndUpdateAddress(mobile, token, user_id, address_id, name, phone, identification, province_id, city_id, district_id, addressInfo)
                .compose(WebManager.allTransformer());
    }

//    /**
//     * 修改地址页初始化
//     **/
//    public Observable<BaseResponse<AddressInfo>> updateAddressInit(String address_id) {
//        return retrofit.updateAddressInit(mobile, token, user_id, address_id)
//                .compose(WebManager.allTransformer());
//    }
//
//    /**
//     * 商品-订单列表显示
//     **/
//    public Observable<BaseResponse<OrderList>> queryGoodsOrderForm(int currentPage, String order_status) {
//        return retrofit.queryGoodsOrderForm(mobile, token, user_id, "" + currentPage, PAGE_COUNT, order_status)
//                .compose(WebManager.allTransformer());
//    }
//
//    /**
//     * 订单详细显示
//     **/
//    public Observable<BaseResponse<OrderInfoDetail>> queryOrderDetails(String order_id) {
//        return retrofit.queryOrderDetails(mobile, token, user_id, order_id)
//                .compose(WebManager.allTransformer());
//    }


    /**
     * 登录接口
     **/
    public Observable<BaseResponse<UserInfo>> loginCheck(String account, String password, String smscode, String countryCode, String redisKey) {
        return retrofit.loginCheck(mobile, account, password, smscode, redisKey, token, countryCode, "2")
                .compose(WebManager.allTransformer());
    }

//    /**
//     * 发送验证码
//     **/
//    public Observable<BaseResponse<Rediskey>> sendMessageCode(String mobile, String countryCode) {
//        return retrofit.sendMessageCode(mobile, countryCode)
//                .compose(WebManager.allTransformer());
//    }

    /**
     * 删除订单接口
     **/
    public Observable<BaseResponse<Object>> delorder(String orderid) {
        return retrofit.delorder(mobile, token, orderid, user_id)
                .compose(WebManager.allTransformer());
    }

    /**
     * 确认订单和取消订单通用接口
     **/
    public Observable<BaseResponse<Object>> updateOrderStatus(String orderid, String orderStatus) {
        return retrofit.updateOrderStatus(mobile, token, user_id, orderid, orderStatus)
                .compose(WebManager.allTransformer());
    }

//    /**
//     * 我的店铺首页显示
//     **/
//    public Observable<BaseResponse<ShopBasicInfo>> showShopIndex() {
//        return retrofit.showShopIndex(mobile, token, user_id, shop_id)
//                .compose(WebManager.allTransformer());
//    }
//
//    /**
//     * 限时抢购-商品列表
//     **/
//    public Observable<BaseResponse<HotTimeGoods>> showHotTimeGoods(String hotTime_id) {
//        return retrofit.showHotTimeGoods(mobile, token, hotTime_id, shop_id)
//                .compose(WebManager.allTransformer());
//    }
//
//    /**
//     * 限时抢购列表页-头部显示
//     **/
//    public Observable<BaseResponse<HotTimeInfo>> showHotTimeGoodsHead() {
//        return retrofit.showHotTimeGoodsHead()
//                .compose(WebManager.allTransformer());
//    }
//
//    /**
//     * 用户退出登录
//     **/
//    public Observable<BaseResponse<HotTimeGoods>> userLogout() {
//        return retrofit.userLogout(mobile, token)
//                .compose(WebManager.allTransformer());
//    }
//
//    /**
//     * 推荐开店页初始化
//     **/
//    public Observable<BaseResponse<OpenShop>> openShop() {
//        return retrofit.openShop(mobile, token, shop_id)
//                .compose(WebManager.allTransformer());
//    }
//
//    /**
//     * 店主精选商品列表展示
//     **/
//    public Observable<BaseResponse<GoodsList>> showShopGoodsList(String currentPage) {
//        return retrofit.showShopGoodsList(mobile, token, shop_id, currentPage, PAGE_COUNT)
//                .compose(WebManager.allTransformer());
//    }
//
//    /**
//     * 店主精选品牌列表展示
//     **/
//    public Observable<BaseResponse<BrandList>> showShopBrandList(String currentPage) {
//        return retrofit.showShopBrandList(mobile, token, shop_id, currentPage, PAGE_COUNT)
//                .compose(WebManager.allTransformer());
//    }
//
//    /**
//     * 店主精选商品页头部展示
//     **/
//    public Observable<BaseResponse<ShopInfo>> showShopGoodsHead() {
//        return retrofit.showShopGoodsHead(mobile, token, shop_id)
//                .compose(WebManager.allTransformer());
//    }
//
//    /**
//     * 我的页初始化
//     **/
//    public Observable<BaseResponse<ShopInfo>> myinit() {
//        return retrofit.myinit(mobile, token, user_id)
//                .compose(WebManager.allTransformer());
//    }

    /**
     * 支付宝支付接口
     * 未转到UI线程
     **/
    public Observable<BaseResponse<AlipayInfo>> alipayPay(String mobile,String token,String user_id,String order_id) {
        return retrofit.alipayPay(mobile, token, user_id, order_id)
                .compose(WebManager.allTransformer());
    }

//    /**
//     * 店铺访问量页展示
//     **/
//    public Observable<BaseResponse<AccessInfo>> showLookNumber() {
//        return retrofit.showLookNumber(mobile, token, shop_id, user_id)
//                .compose(WebManager.allTransformer());
//    }

    /**
     * 收益管理-页面展示
     **/
    public Observable<BaseResponse<ProfitInfo>> showIncome(String mobile,String token,String user_id,String order_id) {
        return retrofit.showIncome(mobile, token, user_id,order_id)
                .compose(WebManager.allTransformer());
    }


    /**
     * 店铺资料查询
     **/
    public Observable<BaseResponse<ShopInfo>> showUserShopInfo() {
        return retrofit.showUserShopInfo(mobile, token, user_id, shop_id)
                .compose(WebManager.allTransformer());
    }

    /**
     * 店铺资料修改
     **/
    public Observable<BaseResponse<Object>> updateShopInfo(String shopName, String shopInfo) {
        return retrofit.updateShopInfo(mobile, token, user_id, shop_id, shopName, shopInfo)
                .compose(WebManager.allTransformer());
    }

    /**
     * 销售管理-统计图展示
     **/
    public Observable<BaseResponse<SaleInfo>> showSellCount(int dayNumber, String beginTime, String endTime) {
        return retrofit.showSellCount(mobile, token, user_id, shop_id, "" + dayNumber, beginTime, endTime)
                .compose(WebManager.allTransformer());
    }

    /**
     * 消息类型显示
     **/
    public Observable<ListResponse<MsgCategory>> showMessageType() {
        return retrofit.showMessageType(mobile, token, user_id)
                .compose(WebManager.allTransformer());
    }

    /**
     * 会员管理-店主管理列表
     **/
    public Observable<GvgListResponse<MemberInfo>> showShopManList(int currentPage) {
        return retrofit.showShopManList(mobile, token, user_id, "" + currentPage, PAGE_COUNT)
                .compose(WebManager.allTransformer());
    }

    /**
     * 消息列表展示
     **/
    public Observable<BaseResponse<MsgInfoList>> messageList(int currentPage) {
        return retrofit.messageList(mobile, token, user_id, PAGE_COUNT, "" + currentPage)
                .compose(WebManager.allTransformer());
    }

    /**
     * 订单消息列表展示
     **/
    public Observable<BaseResponse<OrderMsfList>> orderMessageList(String currentPage) {
        return retrofit.orderMessageList(mobile, token, user_id, currentPage, PAGE_COUNT)
                .compose(WebManager.allTransformer());
    }


    /**
     * 修改店铺图片
     **/
    public Observable<BaseResponse<Object>> uploadShopImg(File imgFile, String imgType) {
        // 创建 RequestBody，用于封装构建RequestBody
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), imgFile);

        // MultipartBody.Part  和后端约定好Key，这里的partName是用image
        MultipartBody.Part body = MultipartBody.Part.createFormData("imgFile", imgFile.getName(), requestFile);

        return retrofit.uploadShopImg(mobile, token, user_id, body, imgType)
                .compose(WebManager.allTransformer());
    }

    /**
     * 售后列表
     **/
    public Observable<BaseResponse<ReturnOrderList>> queryReturnOrder(int currentPage) {
        return retrofit.queryReturnOrder(mobile, token, user_id, "" + currentPage, PAGE_COUNT)
                .compose(WebManager.allTransformer());
    }

    /**
     * 退货申请
     **/
    public Observable<BaseResponse<Object>> returnOrderApply(String order_id, String returnReason, String returnDesc, String type) {
        return retrofit.returnOrderApply(mobile, token, user_id, order_id, returnReason + " " + returnDesc, type)
                .compose(WebManager.allTransformer());
    }

    /**
     * 邀请记录列表
     **/
    public Observable<BaseResponse<InviteList>> showInviteNote(int currentPage) {
        return retrofit.showInviteNote(mobile, token, user_id, "" + currentPage, PAGE_COUNT)
                .compose(WebManager.allTransformer());
    }

    /**
     * 账单明细列表展示
     **/
    public Observable<BaseResponse<BillList>> showBillList(int currentPage, String type) {
        return retrofit.showBillList(mobile, token, user_id, "" + currentPage, PAGE_COUNT, type)
                .compose(WebManager.allTransformer());
    }

    /**
     * 退货（退款）详情页显示
     **/
    public Observable<BaseResponse<ReturnOrderDetail>> returnOrderShow(String order_id, String return_id, String type) {
        return retrofit.returnOrderShow(mobile, token, user_id, order_id, return_id, type)
                .compose(WebManager.allTransformer());
    }

    /**
     * 退货日志接口
     **/
    public Observable<BaseResponse<ReturnProcess>> queryReturn(String flage, String return_id) {
        return retrofit.queryReturn(mobile, token, user_id, flage, return_id)
                .compose(WebManager.allTransformer());
    }

    /**
     * 微信支付接口
     **/
    public Observable<BaseResponse<WechartPay>> wxPay(String mobile,String token,String user_id,String order_id) {
        return retrofit.wxPay(mobile, token, user_id, order_id)
                .compose(WebManager.allTransformer());
    }

    /**
     * 优惠券列表展示
     **/
    public Observable<ListResponse<CouponInfo>> couponList(String couponStatus, String totalMoney) {
//        return retrofit.couponList(mobile, token, user_id, couponStatus, totalMoney)
        return retrofit.couponList(mobile, token, user_id, couponStatus, totalMoney)
                .compose(WebManager.allTransformer());
    }

    /**
     * 修改密码-手机号验证
     **/
    public Observable<BaseResponse<MobileCheck>> mobileCheck(String rediskey, String smscode, String mobile) {
        return retrofit.mobileCheck(mobile, token, rediskey, smscode, user_id)
                .compose(WebManager.allTransformer());
    }

    /**
     * 修改密码
     **/
    public Observable<BaseResponse<Object>> updatePassWord(String newPassword, String secondPassword, String updateLock) {
        return retrofit.updatePassWord(mobile, token, user_id, newPassword, secondPassword, updateLock)
                .compose(WebManager.allTransformer());
    }

    /**
     * 绑定支付宝初始化
     **/
    public Observable<BaseResponse<AlipayInfo>> showAlipayMessage() {
        return retrofit.showAlipayMessage(mobile, token, user_id)
                .compose(WebManager.allTransformer());
    }

    /**
     * 绑定支付宝
     **/
    public Observable<BaseResponse<Object>> saveAlipayMessage(String trueName, String alipayAccount) {
        return retrofit.saveAlipayMessage(mobile, token, user_id, trueName, alipayAccount)
                .compose(WebManager.allTransformer());
    }

    /**
     * 支付宝（微信）提现接口
     **/
    public Observable<BaseResponse<Object>> alipayDrawMoney(String drawMoney, String paymentType) {
        return retrofit.alipayDrawMoney(mobile, token, user_id, drawMoney, paymentType)
                .compose(WebManager.allTransformer());
    }


    /**
     * 首页-消息提醒（7天内）
     **/
    public Observable<BaseResponse<MessageCount>> messageCall() {
        return retrofit.messageCall(mobile, token, user_id)
                .compose(WebManager.allTransformer());
    }

    /**
     * 首页-读秒图片广告
     **/
    public Observable<BaseResponse<ImgData>> homeActivityImage() {
        return retrofit.homeActivityImage()
                .compose(WebManager.allTransformer());
    }

    /**
     * 销量管理-销量数展示
     **/
    public Observable<BaseResponse<SaleInfo>> showSellNumber(int dayNumber) {
        return retrofit.showSellNumber(mobile, token, user_id, shop_id, "" + dayNumber)
                .compose(WebManager.allTransformer());
    }

    /**
     * 物流列表显示
     **/
    public Observable<ListResponse<TransportInfo>> transportListShow(String order_id) {
        return retrofit.transportListShow(mobile, token, order_id)
                .compose(WebManager.allTransformer());
    }

    /**
     * 物流详情显示
     **/
    public Observable<BaseResponse<TransportDetail>> transportShow(String transportCode, String order_id) {
        return retrofit.transportShow(mobile, token, transportCode, order_id)
                .compose(WebManager.allTransformer());
    }

    /**
     * 修改支付密码
     **/
    public Observable<BaseResponse<Object>> updateTradePassword(String tradepassword, String smscode, String redisKey) {
        return retrofit.updateTradePassword(mobile, token, tradepassword, smscode, redisKey, user_id)
                .compose(WebManager.allTransformer());
    }

    /**
     * 余额支付接口
     **/
    public Observable<BaseResponse<Object>> availablePay(String order_id, String payPassword,String mobile,String token,String user_id) {
        return retrofit.availablePay(mobile, token, user_id, order_id, payPassword)
                .compose(WebManager.allTransformer());
    }

    /**
     * 访问添加接口
     **/
    public Observable<BaseResponse<Object>> saveSession(String goods_id, String brand_id) {
        return retrofit.saveSession(mobile, token, user_id, shop_id, goods_id, brand_id, "1")
                .compose(WebManager.allTransformer());
    }

    /**
     * 分享量添加接口
     **/
    public Observable<BaseResponse<Object>> saveShare(String goods_id, String brand_id) {
        return retrofit.saveShare(mobile, token, shop_id, goods_id, brand_id)
                .compose(WebManager.allTransformer());
    }

    /**
     * 消息列表
     **/
    public Observable<GvgListResponse<JMessageInfo>> getChatMessageContent(int currentPage, String from_id, String target_id, String msg_id) {
        return retrofit.getChatMessageContent(mobile, token, currentPage + "", "20", from_id, target_id, msg_id)
                .compose(WebManager.allTransformer());
    }

//    /**
//     * 保存聊天消息
//     **/
//    public Observable<BaseResponse<Object>> saveChatMessage(Message message) {
//        String text = message.getContentType() == ContentType.text ? ((TextContent) message.getContent()).getText() : null;
//        text = AppUtil.emojiConvert1(text);
//        return retrofit.saveChatMessage(mobile, token,
//                "" + message.getCreateTime(),
//                message.getServerMessageId() + "",
//                message.getContentType().name(),
//                text,
//                message.getContentType() == ContentType.image ? URLEncoder.encode(((ImageContent) message.getContent()).getMediaID()) : null,
//                message.getFromUser().getUserName(),
//                "app",
//                message.getTargetID()
//        ).compose(WebManager.allTransformer());
//    }

    /**
     * 代付-初始化分享页面
     **/
    public Observable<BaseResponse<FriendPayInfo>> intianotherPayMessage(String order_id) {
        return retrofit.intianotherPayMessage(mobile, token, user_id, order_id)
                .compose(WebManager.allTransformer());
    }

    /**
     * 银联支付
     **/
    public Observable<BaseResponse<String>> unionAppPay(String order_id) {
        return retrofit.unionAppPay(mobile, token, user_id, order_id)
                .compose(WebManager.allTransformer());
    }


}